package com.example.demo.exception;

import com.example.demo.pojo.ResponseMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice               //RestControllerAdvice 是做统一的错误处理的.
public class GlobalExceptionHandlerAdvice {
        Logger logger = LoggerFactory.getLogger(GlobalExceptionHandlerAdvice.class);

        @ExceptionHandler({Exception.class})      // Exception.class (反射), 这里表示所有的异常都在这处理. 没有明确到底是什么Exception
        public ResponseMessage handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
            //  这里写了3个, 不一定都用到, 都写上, Exception: 通用err, request和response, 请求和响应的错误.
            logger.error("统一异常: ", e);
            return new ResponseMessage( "err", 500, null);
        }
}
